Skip to content

feat(base): add parse_fmt to turn a MATLAB-style format string into options - #321

Open
petercorke wants to merge 1 commit into
rai-opensource:masterfrom
petercorke:feat/parse-fmt
Open

petercorke wants to merge 1 commit into
rai-opensource:masterfrom
petercorke:feat/parse-fmt

Conversation

@petercorke

Copy link
Copy Markdown
Collaborator

Adds spatialmath.base.parse_fmt(fmt), which turns a MATLAB-style format string such as "r--", "go" or "C2:" into a dict of Matplotlib options.

Why. The 2D plot functions here (plot_point, plot_polygon, plot_circle, plot_ellipse) take the string as *fmt and hand it to plt.plot. That works, but there is no way to put a default underneath it, because a default color= keyword would override the string's color. Callers that want defaults (the Machine Vision Toolbox and the Robotics Toolbox draw things in a default color and width) have each written their own parser, and they differ. This gives one that they can share.

What it returns. Only the parts the string sets, as color, marker and linestyle keys, so defaults | parse_fmt(fmt) | kwargs works as expected:

>>> parse_fmt("r--")
{'color': 'r', 'linestyle': '--'}
>>> parse_fmt("--")
{'linestyle': '--'}
>>> parse_fmt("go")
{'color': 'g', 'marker': 'o', 'linestyle': 'None'}

It follows Matplotlib's rules for plot(): a marker with no line style means no line, C0 to Cn are cycle colors, a string that is a whole color ("red", "#ff8000", "0.5") is a color, and "1" is a marker, not a gray level. It raises ValueError for two colors, two markers, two line styles or an unknown character. The one difference is that it refuses two colors when one is a cycle color ("bC0"), which plot() accepts and quietly takes the last of.

It does not accept the unusual forms that Matplotlib allows (a space as a line style, "None", numeric markers).

Tests. tests/base/test_parse_fmt.py compares the result with Matplotlib's own _process_plot_format for every color, marker and line style in every order (about 6,600 valid strings), every valid pair, whole-string colors, and a set of invalid strings that both refuse. It also checks that the result is accepted by plot() and gives the same line as the string. black is clean and tests/base passes (240 passed, 1 skipped).

Nothing else changes: the existing plot functions are untouched. The function is added to the lazy imports in base/__init__.py and to func_2d_graphics.rst.

AI-assisted: written with Claude Code (Claude Sonnet 5.5) and reviewed by the maintainer.

🤖 Generated with Claude Code

…ptions

Parse strings such as 'r--', 'go' or 'C2:' into color, marker and linestyle
keys, with only the parts the string sets, so the result can be merged over
defaults. Follows the rules of Matplotlib's plot(): a marker with no
linestyle means no line. Tested against Matplotlib's own parser.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant